package models

import (
	"github.com/Unknwon/com"
	"github.com/gin-gonic/gin"
	"time"
)

type Distributor struct {
	ID int `json:"id"`
	Maibao float64 `json:"maibao"`
	MaibaoAll float64 `json:"maibao_all"`
	Reward float64 `json:"reward"`
	RewardAll float64 `json:"reward_all"`
	Region string  `json:"region"`
	RegionId int `json:"region_id"`
	Phone string `json:"phone"`
	Password string `json:"-"`
	PayPwd string `json:"-"`
	Name string `json:"name"`
	Avatar string `json:"avatar"`
	ApiToken string `json:"-"`
	Status int `json:"status"`
	IdCard string `json:"id_card"`
	IdCardImgFront string `json:"id_card_img_front"`
	IdCardImgBack string `json:"id_card_img_back"`
	BankCardNo string `json:"bank_card_no"`
	BankName string `json:"bank_name"`
	BankUserName string `json:"bank_user_name"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

func (d *Distributor) TableName() string {
	return "maibao_distributor"
}

func DistributorList(page int, limit int, c *gin.Context) (interface{}, *Paginator) {
	var res []*struct{
		ID int `json:"id"`
		Region string `json:"region"`
		Name string `json:"name"`
		Phone string `json:"phone"`
		Maibao float64 `json:"maibao"`
		MaibaoAll float64 `json:"maibao_all"`
		CreatedAt time.Time `json:"created_at"`
		Status int `json:"status"`
	}
	m := DB.Model(&Distributor{}).Select("id,region,bank_user_name as `name`,phone,maibao,maibao_all,created_at,status")
	if status := c.Query("status"); status != "" {
		m = m.Where("status=?", status)
	}
	if region := c.Query("region"); region != "" {
		m = m.Where("region_id=?", region)
	}
	if phone := c.Query("phone"); phone != "" {
		m = m.Where("phone=?",phone)
	}
	if startTime, isExist := c.GetQuery("begin_time"); isExist && startTime != "" {
		m = m.Where("created_at > ?", startTime)
	}
	if endTime, isExist := c.GetQuery("end_time"); isExist && endTime != "" {
		m = m.Where("created_at < ?", endTime)
	}
	var count int
	m.Count(&count)
	paginate := NewPage(page, limit, count)
	err := m.Scan(&res).Error
	if err != nil {
		return nil, nil
	}
	return res, &paginate
}

func DistributorGet(id int) *Distributor {
	var res Distributor
	DB.Model(&Distributor{}).Where("id=?", id).First(&res)
	return &res
}

func PutForwardListByDistributor(page int, pageSize int, c *gin.Context) (interface{}, *Paginator) {
	var res []*struct{
		ID int `json:"id"`
		DID int `json:"did"`
		Region string `json:"region"`
		Name string `json:"name"`
		Phone string `json:"phone"`
		BankName string `json:"bank_name"`
		BankCardNo string `json:"bank_card_no"`
		Amount float64 `json:"amount"`
		CreatedAt time.Time `json:"created_at"`
		Status int `json:"status"`
	}

	db := DB.Table("maibao_distributor_cash as c").
		Select("c.id,d.id as did,d.region,d.name,d.phone,d.bank_name,d.bank_card_no,c.amount,c.created_at,c.status").
		Joins("left join maibao_distributor as d on d.id=c.d_id")

	if status := c.Query("status"); status != ""{
		statusi := com.StrTo(status).MustInt()
		db = db.Where("c.status=?", statusi)
	}
	if id := c.Query("status"); id != "" {
		idi := com.StrTo(id).MustInt()
		db = db.Where("d.id=?", idi)
	}
	var count int
	db.Count(&count)
	paginate := NewPage(page, pageSize, count)
	db.Offset(paginate.Offset).Limit(paginate.Limit).Find(&res)
	return &res, &paginate
}
/**
	收益记录
 */
func DistributorProfitLog(page int, pageSize int, c *gin.Context) (interface{}, *Paginator) {
	var res []*struct{
		ID int `json:"id"`
		Region string `json:"region"`
		Name string `json:"name"`
		Phone string `json:"phone"`
		RewardAll float64 `json:"reward_all"`
		Cash float64 `json:"cash"`
		Reward float64 `json:"reward"`
	}

	db := DB.Model(&Distributor{})
	if region := c.Query("region"); region != "" {
		regionId := com.StrTo(region).MustInt()
		db = db.Where("region_id=?", regionId)
	}
	if name := c.Query("name"); name != "" {
		db = db.Where("name=?", name)
	}
	if phone := c.Query("phone"); phone != "" {
		db = db.Where("phone=?", phone)
	}

	var count int
	db.Count(&count)
	paginate := NewPage(page, pageSize, count)
	db.Offset(paginate.Offset).Limit(paginate.Limit).Find(&res)
	return &res, &paginate
}

func DistributorProfitLogList(page int, pageSize int, c *gin.Context, id int) (interface{}, *Paginator) {
	var res []*struct{
		Name string `json:"name"`
		Phone string `json:"phone"`
		Cate int `json:"cate"`
		Amount float64 `json:"amount"`
		CreatedAt time.Time `json:"created_at"`
	}

	db := DB.Table("maibao_distributor_log").Where("distributor_id=?", id).Where("type=1")

	if name := c.Query("name"); name != "" {
		db = db.Where("name=?", name)
	}

	if startTime, isExist := c.GetQuery("begin_time"); isExist && startTime != "" {
		db = db.Where("created_at > ?", startTime)
	}
	if endTime, isExist := c.GetQuery("end_time"); isExist && endTime != "" {
		db = db.Where("created_at < ?", endTime)
	}
	var count int
	db.Count(&count)
	paginate := NewPage(page, pageSize, count)
	db.Offset(paginate.Offset).Limit(paginate.Limit).Find(&res)
	return &res, &paginate
}